<?php
namespace Common\Models;
use App\Api\Controllers\TurnController;
use Common\BaseModel;
use Common\Common;

class  Material extends BaseModel
{
	public $res;
	public $expire;
    public function initialize()
    {
        parent::initialize();
        $this->setSource('source_material');
    }
	public function FindByid($id){
		//$condition['conditions']="is_defalut =1";
		//  $condition['columns']='id';
		$res=$this->findFirst($id);
		if(empty($res)){
			return false;
		}else{
			return  $res;
		}
	}

	//排序
	public function findbyidsarr_sort($ids){
		$conditions = "id in($ids)";
		$get_list = $this->find(array(
				$conditions,
				'order'=>'sort desc,id asc',
		))->toarray();
		$id_arr = array_column($get_list,'id');
		$ids = implode(',',$id_arr);
		return $ids;
	}


	//拿列表
	public function getListByIds($ids,$user_info=[],$is_color= 0){
		if(!$ids){
		    return [];
		}
		$ids = $this->findbyidsarr_sort($ids);
		$ids=explode(',',$ids);
		$data=[];
		foreach($ids as $k =>$v){
			$res=$this->FindByid($v);
			if($res){
				$res=$res->toArray();
				$arr=[
					'name'=>$res['name'],
					'path'=>$res['path'],
					'change_style'=>$res['change_style'],
				];
				if($is_color == 0){
					unset($res['start_color'],$res['end_color']);
				}else{
    				$arr['start_color']=$res['start_color'];
    				$arr['end_color']=$res['end_color'];
				}
				$route=[
					'id'=>$res['id'],
					'type'=>$res['type'],
					'path'=>$res['app_path'],
					'is_tb_atuh'=>$res['is_tb_atuh'],
					'is_need_login'=>$res['is_need_login'],
					'title'=>$res['title'],
					'url'=>$res['url'],
					'data'=>$res['app_data']?$res['app_data']:'',
                    'relationId'=>isset($res['relationId']) && $res['relationId'] ? $res['relationId'] : '0',
					'notice'=>null,
				];
                if($user_info) {
//                    $route = $this->route_analysis($route, $user_info);
                }
				$arr['route']=$route;
				$data[]=$arr;
			}
		}
		return $data;
	}




    /**
     * 路由分析
     * @param $route
     * @param $user_info
     * @return mixed
     * author:dengjing
     * date:2020/11/30 5:08 下午
     */
    public function route_analysis($route,$user_info=[]){
        //打开h5拼接秘钥
        if(($route['path'] == 'H5BarAlpha'|| $route['path'] == 'H5' || $route['path'] == 'H5UserId') && !(strpos($route['url'],'dfh') !== false || strpos($route['url'],'tfh') !== false || strpos($route['url'],'pfh') !== false )){
            if(strpos($route['url'],'?') !== false){
                $route['url'] = (string)$route['url'].'&h5sign='.Common::h5fcrypt($user_info['id']);
            }else{
                $route['url'] = (string)$route['url'].'?h5sign='.Common::h5fcrypt($user_info['id']);
            }
        }


        //打开活动 淘宝活动
        if($route['path'] == 'AwakeTbApp'){
            //打开活动
            if(strpos($route['url'],'open_taobao_') !== false){
                $av_arr = explode('_',$route['url']);
                if(isset($user_info['tb_rid']) && !empty($user_info['tb_rid'])){
                    $path = str_replace('public', "app/plugins/Taobao.php", getcwd());
                    require_once $path;
                    $taobao = new \Taobao($this->di->getConfig()->services->union->tbUnion->toarray());
                    $activity_link = $taobao->activitylink_new($av_arr[2],$user_info['tb_rid']);
                    $route['url'] = isset($activity_link['code']) && $activity_link['code'] ? $activity_link['data'] : $route['url'];
                }
            }
        }

        //多麦转链
        $duomai_url = 'track.php';
        if (strpos($route['url'],$duomai_url) !== false) {
            $route['url']  = $route['url'] . "&euid={$user_info['id']}";
        }


        //京东活动转链
        if($route['path'] == 'AWakeJdApp'){
            $path = str_replace('public', "plugins/Jd.php", getcwd());
            require_once $path;
            $jd = new \Jd($this->di->getConfig()->services->union->jdUnion->toarray());
            $res = $jd->get_goods_link($user_info['phone'],$route['url']);
            if($res['status']){
                $route['url'] = $res['msg'];
            }
        }


        //拼多多活动处理
        $pdd_url='pinduoduo://com.xunmeng.pinduoduo';
        if(strpos($route['url'],$pdd_url) !== false){
            $path = str_replace('public', "plugins/Pdd.php", getcwd());
            require_once $path;
            $pdd = new \Pdd($this->di->getConfig()->services->union->pddUnion->toarray());
            $params_arr = Common::convertUrlQuery($route['url']);
            $gen=[];
            if(isset($params_arr['method']) && $params_arr['method']=='gen'){                       //频道页转链
                if(isset($params_arr['resource_type']) && !empty($params_arr['resource_type'])){
                    $gen['id']=$params_arr['resource_type'];
                }elseif(isset($params_arr['url']) && !empty($params_arr['url'])){
                    $gen['url']=$params_arr['url'];
                }else{
                    $gen['id']=1;
                }
                if (empty($user_info['pdd_pid'])) {
                    $turn = new TurnController();
                    $turn->created_pid($user_info);
                    $user = new User();
                    $user_info = $user->find_by_firstId($user_info['id']);
                }
                $res = $pdd->site_turn($user_info['pdd_pid'],$gen);
                if($res['code']){
                    $route['url'] = str_replace("https://mobile.yangkeduo.com", "pinduoduo://com.xunmeng.pinduoduo", $res['data']['url']);
                }
            }

            if(isset($params_arr['method']) && $params_arr['method']=='generate'){                      //主题页转链
                if(isset($params_arr['theme_ids']) && !empty($params_arr['theme_ids'])){
                    $gen['theme_ids']=$params_arr['theme_ids'];
                }else{
                    $gen['theme_ids']='7609';
                }
                if (empty($user_info['pdd_pid'])) {
                    $turn = new TurnController();
                    $turn->created_pid($user_info);
                    $user = new User();
                    $user_info = $user->find_by_firstId($user_info['id']);
                }
                $res = $pdd->site_generate($user_info['pdd_pid'],$gen);
                $route['url'] = str_replace("https://mobile.yangkeduo.com", "pinduoduo://com.xunmeng.pinduoduo", $res['data']['url']);
            }
        }

        return $route;
    }


}

